package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostApplyDao;
import ysq.xyg.host.model.po.HostApply;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostApplyVo;
import ysq.xyg.host.service.HostApplyService;

import java.util.Date;
import java.util.List;

/**
 * 房产-兼职经纪人service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:26:16
 */
@Service("hostApplyService")
public class HostApplyServiceImpl extends BaseServiceImpl<HostApply> implements HostApplyService {

    @Autowired
    private HostApplyDao hostApplyDao;

    @Autowired
    public void setBaseDao(HostApplyDao hostApplyDao) {
        this.baseDao = hostApplyDao;
    }

    /**
     * 分页查询
     *
     * @param hostApplyVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:26:16
     */
    @Override
    public ReturnEntity page(HostApplyVo hostApplyVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostApply> list = acceptList(hostApplyVo);
        PageInfo<HostApply> page = new PageInfo<HostApply>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostApplyVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:26:16
     */
    @Override
    public ReturnEntity addOrUpdate(HostApplyVo hostApplyVo, SysUser currentUser) {
        boolean isUpate = true;
        HostApply hostApply;
        if (StrUtil.isEmpty(hostApplyVo.getId())) {
            hostApply = new HostApply(currentUser);
            isUpate = false;
        } else {
            hostApply = selectByPrimaryKey(hostApplyVo.getId());
            if (ObjectUtil.isNull(hostApply)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostApply.setUpdateTime(new Date());
        }
        hostApply.setAccountId(hostApplyVo.getAccountId());
        hostApply.setMemberId(hostApplyVo.getMemberId());
        hostApply.setPhone(hostApplyVo.getPhone());
        hostApply.setSex(hostApplyVo.getSex());
        hostApply.setName(hostApplyVo.getName());
        hostApply.setCardNo(hostApplyVo.getCardNo());
        hostApply.setBankNo(hostApplyVo.getBankNo());
        hostApply.setPassword(hostApplyVo.getPassword());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostApply);
        } else {
            num = insert(hostApply);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:26:16
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostApply hostApply = selectByPrimaryKey(id);
        if (hostApply != null) {
            hostApply.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostApply.setUpdateTime(new Date());
            int num = updateByPrimaryKey(hostApply);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostApplyVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:26:16
     */
    @Override
    public List<HostApply> acceptList(HostApplyVo hostApplyVo){
        Example example = new Example(HostApply.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostApplyVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostApplyVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostApplyVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostApplyVo.getAccountId());
        }
        example.setOrderByClause(" update_time desc");
        return hostApplyDao.selectByExample(example);
    }
}
